const state = {
  visitedViews: [],
  cachedViews: []
};
//内部方法 fn:(state, view) => {}
const mutations = {
  ADD_CACHED_VIEW: (state, view) => {
    if (state.cachedViews.includes(view.name)) return;
    if (!view.meta.noCache) {
      state.cachedViews.push(view.name);
    }
  },
  DEL_CACHED_VIEW: (state, view) => {
    const index = state.cachedViews.indexOf(view.name);
    index > -1 && state.cachedViews.splice(index, 1);
  },
  DEL_OTHERS_CACHED_VIEWS: (state, view) => {
    const index = state.cachedViews.indexOf(view.name);
    if (index > -1) {
      state.cachedViews = state.cachedViews.slice(index, index + 1);
    } else {
      // if index = -1, there is no cached tags
      state.cachedViews = [];
    }
  },
  DEL_ALL_CACHED_VIEWS: state => {
    state.cachedViews = [];
  }
};
//对外方法 fn({ commit },value)
const actions = {
  addCachedView({ commit }, view) {
    commit("ADD_CACHED_VIEW", view);
  },
  delCachedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit("DEL_CACHED_VIEW", view);
      resolve([...state.cachedViews]);
    });
  }
};
const getters = {
  cachedViews: state => state.cachedViews
};

/**
 * @description 页签相关
 */
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
